#include "detail.hpp"
#include "message.hpp"
#include "dispatcher.hpp"
#include "net.hpp"
#include <iostream>
void onRpcResponce(const bitrpc::BaseConnection::ptr &conn, bitrpc::RpcResponse::ptr &msg)
{
    std::cout << "收到了rpc请求";
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}
void onTopicRespoce(const bitrpc::BaseConnection::ptr &conn, bitrpc::TopicResponse::ptr &msg)
{
    std::cout << "收到了topic请求";
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}
int main()
{
    auto dispather = std::make_shared<bitrpc::Dispather>();
    dispather->registerHander<bitrpc::RpcResponse>(bitrpc::MType::RSP_RPC, onRpcResponce);
    dispather->registerHander<bitrpc::TopicResponse>(bitrpc::MType::RSP_TOPIC, onTopicRespoce);
    
    auto client = bitrpc::ClientFactory::create("127.0.0.1", 9090);
    auto message_cb = std::bind(&bitrpc::Dispather::onMessage, dispather.get(), std::placeholders::_1, std::placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();

    auto rpc_req = bitrpc::MessageFactory::create<bitrpc::RpcRequest>();
    rpc_req->setId("11111");
    rpc_req->setMType(bitrpc::MType::REQ_RPC);
    rpc_req->setMethod("ADD");
    Json::Value param;
    param["num1"] = 1;
    param["num2"] = 2;
    rpc_req->setParams(param);
    client->send(rpc_req);

    auto topic_req = bitrpc::MessageFactory::create<bitrpc::TopicRequest>();
    topic_req->setId("22222");
    topic_req->setMType(bitrpc::MType::REQ_TOPIC);
    topic_req->setOptype(bitrpc::TopicOptype::TOPIC_CREATE);
    topic_req->setTopicKey("news");
    client->send(topic_req);

    sleep(10);

    client->shutdown();
    return 0;
}
